package com.borrow.jiemihua.mainUI.activity.LoginRegister;

import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.base.BaseActivity;
import com.borrow.jiemihua.base.BaseApplication;
import com.borrow.jiemihua.base.BaseConstant;
import com.borrow.jiemihua.base.URLConstant;
import com.borrow.jiemihua.mainUI.MainActivity;
import com.borrow.jiemihua.push.PushProxy;
import com.borrow.jiemihua.utils.ConfirmUtils;
import com.borrow.jiemihua.utils.SharedPreferencesUtils;
import com.borrow.jiemihua.utils.xutils3.MyCallBack;
import com.borrow.jiemihua.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import cc.duduhuo.custoast.CusToast;

@ContentView(R.layout.activity_password_login)
public class PasswordLoginActivity extends BaseActivity {
    @ViewInject(R.id.tv_top_title)
    private TextView tv_top_title;
    @ViewInject(R.id.tv_forgetPass)
    private TextView tv_forgetPass;
    @ViewInject(R.id.et_phone)
    private EditText et_phone;
    @ViewInject(R.id.et_password)
    private EditText et_password;
    @ViewInject(R.id.rl_tv_right)
    private RelativeLayout rl_tv_right;
    @ViewInject(R.id.tv_right)
    private TextView tv_right;

    private String  phone;
    private String  password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        BaseApplication.addDestoryActivity(this,"login1");
        initData();
        initView();
    }

    @Override
    protected void initData() {
        tv_forgetPass.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        tv_top_title.setText("密码登录");
        tv_right.setText("手机登录");
        rl_tv_right.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initView() {

    }
    @Event(value = {R.id.imgbtn_left,R.id._tv_login_ok,R.id.ll_login_code,R.id.tv_forgetPass},type = View.OnClickListener.class)
    private void getEvent(View view) {
//        Intent intent = null;
        switch (view.getId()) {
            case R.id.imgbtn_left:
                finish();
                break;

            case R.id.ll_login_code:
                finish();
                break;
            case R.id.tv_forgetPass:
                jumpActivity(ForgetPasswordActivity.class,null,false);
                break;
            case R.id._tv_login_ok:
                phone = et_phone.getText().toString().trim();
                password = et_password.getText().toString().trim();
                if (ConfirmUtils.isMobileNO(phone) && password.length() >= 6) {
                    upLogin();
                } else if (!ConfirmUtils.isMobileNO(phone)) {
                    CusToast.showToast("请输入正确的手机号");
                    return;
                }else if (password.length() < 6) {
                    CusToast.showToast("请输入不少于6位数的密码");
                    return;
                }
                break;
            default:break;
        }

    }
    private void upLogin(){
        Map<String,Object> map=new HashMap<>();
        map.put("account",phone);
        map.put("loginTypeStr","cellphone");//登陆类型
        map.put("password",password);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("--密码登陆参数--" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(URLConstant.LOGINJ,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
//                Log.i("==密码登陆--",result);
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    String msg=jsonObject.optString("msg");
                    if("1".equals(jsonObject.optString("isSuccess"))){
                        String data=jsonObject.optString("data");
                        JSONObject jsonObject2=new JSONObject(data);
                        String userId=jsonObject2.optString("userId");
//                        Log.i("==密码登陆--userId",userId);
                        //个推绑定
                        PushProxy.getPushManager().bindAlias(PasswordLoginActivity.this.getApplicationContext(), userId);
                        SharedPreferencesUtils.saveString(PasswordLoginActivity.this, BaseConstant.SPConstant.USERID, userId);
                        SharedPreferencesUtils.saveString(PasswordLoginActivity.this, BaseConstant.SPConstant.MOBILEPHONE, phone);
                        SharedPreferencesUtils.saveString(PasswordLoginActivity.this, BaseConstant.SPConstant.PAYPASSWORD, password);
                        CusToast.showToast("登录成功");
                        BaseApplication.destoryActivity("login1");
                        jumpActivity(MainActivity.class,null,true);
                    }else{
                        CusToast.showToast(msg);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });
    }
}
